package com.jzcn.admin.controller.enterprise;

import com.alibaba.excel.EasyExcel;
import com.jzcn.admin.annotation.ServerLog;
import com.jzcn.admin.base.BaseController;
import com.jzcn.common.dto.enterprise.EnterpriseUserDTO;
import com.jzcn.common.dto.enterprise.EnterpriseUserExcelImportDTO;
import com.jzcn.common.dto.enterprise.OrgRecordDTO;
import com.jzcn.common.dto.enterprise.SearchEnterpriseUserDTO;
import com.jzcn.common.dto.system.position.PositionRecordDTO;
import com.jzcn.common.response.ResponseResult;
import com.jzcn.common.utils.JzPage;
import com.jzcn.domain.EntPosition;
import com.jzcn.service.EntEmployeeService;
import com.jzcn.service.EntOrgService;
import com.jzcn.service.EntPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/enterprise/user")
@Api(value = "EmployeeController", tags = "员工管理")
public class EmployeeController extends BaseController {

    static Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @Resource
    EntEmployeeService entEmployeeService;

    @Resource
    EntOrgService entOrgService;

    @Resource
    EntPositionService entPositionService;

    @ApiOperation("员工查询")
    @GetMapping("/page")
    @RequiresPermissions("employee:user:page")
    @ServerLog(value ="员工查询", code= "employee:user:page")
    public ResponseResult<JzPage<EnterpriseUserDTO>> page(SearchEnterpriseUserDTO dto) {
        return ResponseResult.success(entEmployeeService.listPage(dto));
    }

    @ApiOperation("员工列表")
    @GetMapping("/list")
    public ResponseResult<List<EnterpriseUserDTO>> list(@RequestParam(required = false) List<Integer> ids) {
        return ResponseResult.success(entEmployeeService.dataList(ids));
    }

    @ApiOperation("获取部门列表")
    @GetMapping("/org/list")
    public ResponseResult<List<OrgRecordDTO>> orgList() {
        return ResponseResult.success(entOrgService.orgList());
    }


    @ApiOperation("获取岗位列表")
    @GetMapping("/pos/list")
    public ResponseResult<List<PositionRecordDTO>> posList() {
        return ResponseResult.success(entPositionService.posEmployeeList());
    }

    @ApiOperation("新增员工")
    @PostMapping("/create")
    @RequiresPermissions("employee:user:create")
    @ServerLog(value ="新增员", code= "employee:user:create")
    public ResponseResult<Boolean> create(@RequestBody EnterpriseUserDTO dto) {
        return ResponseResult.success(entEmployeeService.create(dto));
    }


    @ApiOperation("编辑员工")
    @PutMapping("/update/{id}")
    @RequiresPermissions("employee:user:update")
    @ServerLog(value ="编辑员工", code= "employee:user:update")
    public ResponseResult<Boolean> update(@PathVariable("id") Integer id, @RequestBody EnterpriseUserDTO dto) {
        dto.setId(id);
        return ResponseResult.success(entEmployeeService.update(dto));
    }
    @ApiOperation("删除员工")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("employee:user:delete")
    @ServerLog(value ="删除员工", code= "employee:user:delete")
    public ResponseResult<Boolean> delete(@PathVariable("id") Integer id) {
        return ResponseResult.success(entEmployeeService.delete(id));
    }

    @ApiOperation("员工详情")
    @GetMapping("/info")
    @RequiresPermissions("employee:user:info")
    @ServerLog(value ="员工详情", code= "employee:user:info")
    public ResponseResult<EnterpriseUserDTO> info(@RequestParam("id") Integer id) {
        return ResponseResult.success(entEmployeeService.getByEnterprise(id));
    }



    /**
     * 文件上传
     * <p>1. 创建excel对应的实体对象 参照
     * <p>2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照
     * <p>3. 直接读即可
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public ResponseResult<Boolean> upload(@RequestPart("file") MultipartFile file) throws IOException {
        List<EnterpriseUserExcelImportDTO> excelList = EasyExcel.read(file.getInputStream()).head(EnterpriseUserExcelImportDTO.class).sheet().doReadSync();
        return ResponseResult.success(entEmployeeService.userExcelImport(excelList));
    }
}
